import {
  NATIVE_UPDATE_FILTER_CONDITIONS,
  NATIVE_UPDATE_TASKS,
  NATIVE_CHANGE_LOADING,
  WEB_UPDATE_TREND,
  WEB_UPDATE_VERSION
} from "./reducer";
import {fetchPageSearchApi, fetchPageListInitData, fetchPageTrendInitData, fetchVersionApi} from "../../../common/api";

export function fetchInitData() {
  return (dispatch) => {

    return fetchPageListInitData()
      .then(data => {
        dispatch({
          type: NATIVE_UPDATE_TASKS,
          tasks: data
        })
      })
  }
}

export function updateFilterConditions(newFilterConditions) {
  return (dispatch, getState) => {
    dispatch({
      type: NATIVE_UPDATE_FILTER_CONDITIONS,
      filterConditions: newFilterConditions
    });
  };
}

export function changeLoading(loading) {
  return (dispatch, getState) => {
    dispatch({
      type: NATIVE_CHANGE_LOADING,
      loading: loading
    });
  };
}

export function fetchTasks({deviceId, platform, page, version, beginTime, endTime, pageNum, pageSize}) {
  return (dispatch, getState) => {
    return fetchPageSearchApi(deviceId, platform, page, version, beginTime, endTime, pageNum, pageSize)
      .then(data => {
        dispatch({
          type: NATIVE_UPDATE_TASKS,
          tasks: data
        });
      })
  };
}

// 获得卡顿趋势图表数据
export function fetchTrendData({deviceId, platform, page, beginTime, endTime, pageNum, pageSize}) {
  return (dispatch) => {
    return fetchPageTrendInitData(deviceId, platform, page, beginTime, endTime, pageNum, pageSize)
      .then(data => {
        console.log(data);
        dispatch({
          type: WEB_UPDATE_TREND,
          trendData: data
        })
      })
  }
}

// 获得版本号
export function fetchVersion() {
  return (dispatch) => {
    return fetchVersionApi()
      .then(data => {
        dispatch({
          type: WEB_UPDATE_VERSION,
          versionOptions: data
        })
      })
  }
}